package yl.hs.bmipfull.viewmodels;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import yl.hs.bmipfull.utils.Tl;

import java.lang.reflect.Field;
import java.util.*;

/**
 * Pagination
 * @category
 * @author 谢景广
 */
@ApiModel(value="分页查询请求模型",description = "分页查询请求模型")
public class PageQuery {

    public String[] getSortSides() {
        return sortSides;
    }

    public void setSortSides(String[] sortSides) {
        this.sortSides = sortSides;
    }

    @ApiModelProperty(name = "排序",required = true, value = "数据排序")
    private String[] sortSides;

    public Map<String, Object> getQuery() {
        return query;
    }

    public String getViewSN() {
        return viewSN;
    }

    public void setViewSN(String viewSN) {
        this.viewSN = viewSN;
    }

    @ApiModelProperty(name = "视图编号",required = false, value = "视图编号")
    private String viewSN;



    public void setQuery(Map<String, Object> query) {
        this.query = query;
    }

    public PageQuery() {
        this.query = new HashMap<>();
    }
    public PageQuery(Map<String,Object> map) {
        this.query = map;
    }
    @ApiModelProperty(name = "过滤条件",required = false, value = "过滤条件")
    private List<QueryFilter> filters;

    public List<QueryFilter> getFilters() {
        return filters;
    }

    public void setFilters(List<QueryFilter> filters) {
        this.filters = filters;
    }


    @ApiModelProperty(name = "数据查询条件",required = true, value = "数据查询条件")
    private Map<String,Object> query;

    public void checkSortSides(Class<?> classType){
        checkSortSides(classType,null);
    }

    public void checkSortSides(Class<?> classType,String defaultSort) {
        var newSides = new ArrayList<String>();

        if(this.sortSides!=null && this.sortSides.length>0) {
            var fields = classType.getDeclaredFields();
            for (var item : this.sortSides) {
                if (!Tl.isEmpty(item.trim())) {
                    var sideArray = item.split(" ");
                    if (sideArray.length > 0) {
                        StringBuilder sb = new StringBuilder();
                        var fieldName = sideArray[0];
                        if (classExistsField(fieldName, fields)) {
                            sb.append(fieldName);
                            if (sideArray.length > 1) {
                                sb.append(" ");
                                var sortType = sideArray[1].toLowerCase();
                                if ("desc".equals(sortType)) {
                                    sb.append("DESC");
                                } else {
                                    sb.append("ASC");
                                }
                            } else {
                                sb.append(" ASC");
                            }
                        }
                        newSides.add(sb.toString());
                    }
                }
            }
        }

       if(newSides.size()==0){
           if(Tl.isEmpty(defaultSort)) {
               newSides.add("id desc");
           }
           else
           {
               newSides.add(defaultSort);
           }
       }
       this.sortSides = newSides.toArray(new String[newSides.size()]);
        this.query.put("sortKey", String.join(",", sortSides));
    }
     static boolean classExistsField(String fieldName, Field[] fields){
       return Tl.filedExists(fields,fieldName);
    }


}
